package gen;

import java.io.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.*;
import org.w3c.dom.*;

/**
 * Simple Demo to show the power of YQL
 * @see http://idojava.blogspot.com/
 * @author Green Ido
 */
public class YqlDemo {

	/**
	 * Find 'food' places for the JPR
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		String request = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20answers.search%20where%20query%20%3D%20%22java%22";

                Request req = new Request();
                InputStream retorno = req.request(request, new String[0][0]);
           

			// Process response
			Document response = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(retorno);
					

			XPathFactory factory = XPathFactory.newInstance();
			XPath xPath = factory.newXPath();
			// Get all search Result nodes
			NodeList nodes = (NodeList) xPath.evaluate("query/results/Question",
					response, XPathConstants.NODESET);
			int nodeCount = nodes.getLength();
			// iterate over search Result nodes
			for (int i = 0; i < nodeCount; i++) {
				// Get each xpath expression as a string
				String subject = (String) xPath.evaluate("Subject", nodes.item(i),XPathConstants.STRING);
				String content = (String) xPath.evaluate("Content", nodes.item(i), XPathConstants.STRING);
				String url = (String) xPath.evaluate("Link", nodes.item(i),XPathConstants.STRING);
				// print out the Title, Summary, and URL for each search result
				System.out.println("Titulo: " + subject);
				System.out.println("Conteudo: " + content);
				System.out.println("URL: " + url);
				System.out.println("-----------");

			
		}
	}

}
